package cn.itcast.controller;


import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Permission;
import cn.itcast.service.PermissionService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("permission")
@Slf4j
public class PermissionController {

    @Reference
    private PermissionService permissionService;


    /**
     * 分页查询权限管理列表
     * @param queryPageBean 查询条件
     * @return
     */
    @GetMapping
    public Result findPage(QueryPageBean queryPageBean){
        System.out.println("permissionService = " + permissionService);
        try {
            PageResult pageResult = permissionService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, pageResult);
        } catch (Exception e) {
            log.error("查询权限管理列表失败", e);
        }
        return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
    }


    /**
     * 增加权限
     * @param permission 权限具体信息
     * @return
     */
    @PostMapping
    public Result handleAdd(@RequestBody Permission permission){
        try {
            Boolean flag = permissionService.handleAdd(permission);
            if (flag){
                return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS, flag);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("增加权限失败", e);
        }
        return new Result(false, MessageConstant.ADD_PERMISSION_FAIL);
    }

    /**
     * 根据id查询单条权限详情
     * @param id 当前权限id
     * @return
     */
    @GetMapping("{id}")
    public Result handleUpdate(@PathVariable("id") Integer id){
        try {
            Permission permission = permissionService.handleUpdate(id);
            if (permission != null){
                return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, permission);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询权限失败", e);
        }
        return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
    }


    /**
     * 编辑权限
     * @param permission 权限具体信息
     * @return
     */
    @PutMapping
    public Result handleEdit(@RequestBody Permission permission){
        try {
            Boolean flag = permissionService.handleEdit(permission);
            if (flag){
                return new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS, flag);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("增加权限失败", e);
        }
        return new Result(false, MessageConstant.EDIT_PERMISSION_FAIL);
    }




    /**
     * 根据id删除权限
     * @param id 当前权限id
     * @return
     */
    @DeleteMapping("{id}")
    public Result handleDelete(@PathVariable("id") Integer id){
        try {
            Boolean flag = permissionService.handleDelete(id);
            if (flag){
                return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS, flag);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("增加权限失败", e);
        }
        return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
    }


    /**
     * 查询所有权限
     * @return
     */
    @GetMapping("all")
    public Result findAll(){
        try {
            List<Permission> list = permissionService.findAll();
            if (list.size() > 0 && list != null){
                return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, list);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询权限失败", e);
        }
        return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
    }


    /**
     * 查询菜单关联的权限
     * @param menuIds 菜单id集合
     * @return
     */
    @GetMapping("menu/association")
    public Result findMenuAssociationPermission(int[] menuIds){

        for (int i = 0; i < menuIds.length; i++) {
            System.out.println(menuIds[i]);
        }
        System.out.println("***********");
        try {
            List<Permission> list = permissionService.findMenuAssociationPermission(menuIds);
            return new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询菜单关联的权限失败", e);
        }
        return new Result(false, MessageConstant.EDIT_PERMISSION_FAIL);
    }



}
